﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using WP_Customer_ShopMob.ViewModels;
using System.Net;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
using WP_Customer_ShopMob.Model;
using System.Runtime.Serialization;

namespace WP_Customer_ShopMob.ViewModels
{
    [DataContract]
    public class MainViewModel : BaseViewModel
    {
        //private Model.Repository repo = new Model.Repository();
        private IRepository repo = Data.GetModel;
        private List<DBO_ShopMob.Article> list = new List<DBO_ShopMob.Article>();

        public MainViewModel()
        {
        }

        private ObservableCollection<CategorieViewModel> listCategories = new ObservableCollection<CategorieViewModel>();
        [DataMember]
        public ObservableCollection<CategorieViewModel> ListCategories
        {
            get { return listCategories; }
            set { listCategories = value; }
        }

        private string search;

        public string Search
        {
            get { return search; }
            set 
            {
                if (search != value)
                {
                    if (value.Length > 2)
                    {
                        search = value;
                        NotifyPropertyChanged("Search");
                        searchArticles();
                    }
                }
            }
        }

        private ObservableCollection<ArticleViewModel> listArticles = new ObservableCollection<ArticleViewModel>();

        public ObservableCollection<ArticleViewModel> ListArticles
        {
            get { return listArticles; }
            set 
            {
                listArticles = value;
            }
        }

        private ClientViewModel compte;
        [DataMember]
        public ClientViewModel Compte
        {
            get { return compte; }
            set { compte = value; }
        }

        private bool isDataLoaded = false;
        [DataMember]
        public bool IsDataLoaded
        {
            get { return isDataLoaded; }
            set { isDataLoaded = value; }
        }

        public void LoadData(string login)
        {
            repo.GetClientParID(login, delegate(string resu, Exception e) {
                if (e == null)
                {
                    DBO_ShopMob.Client cl = JsonConvert.DeserializeObject<DBO_ShopMob.Client>(resu);
                    if (cl != null)
                    {
                        compte = new ClientViewModel(cl.IdClient, cl.Civilite, cl.Nom, cl.Prenom, cl.MotDePasse, cl.Email, cl.Adresse, cl.Cp, cl.Ville, cl.TelFixe, cl.TelMobile, cl.DateNaissance);
                        Helper.IsolatedStorage.Save<ClientViewModel>(compte, "compte");
                    }
                }
            });

            repo.GetCategoriesMere(delegate(string resu, Exception e)
            {
                var des = JsonConvert.DeserializeObject<List<DBO_ShopMob.Categorie>>(resu);
                var categories = Helper.IsolatedStorage.Load<ObservableCollection<CategorieViewModel>>("categories");
                if (categories == null) categories = new ObservableCollection<CategorieViewModel>();
                foreach (var categ in des)
                {
                    CategorieViewModel cvm = new CategorieViewModel() { Details = categ.details, Id = categ.id, IdMere = categ.idMere, Nom = categ.nom, IsDataLoaded = false };
                    ListCategories.Add(cvm);
                    if (categories.SingleOrDefault(x => x.Id == cvm.Id) == null)
                    {
                        categories.Add(cvm);
                    }
                }
                Helper.IsolatedStorage.Save<ObservableCollection<CategorieViewModel>>(categories, "categories");
                IsDataLoaded = true;
            });
            //List<DBO_ShopMob.Categorie> list = new List<DBO_ShopMob.Categorie>();
            //list.Add(new DBO_ShopMob.Categorie() { details = "sport", id = 1, idMere = 0, nom = "Sport" });
            ////list.Add(new DBO_ShopMob.Categorie() { details = "sport", id = 2, idMere = 1, nom = "Football" });
            //list.Add(new DBO_ShopMob.Categorie() { details = "alim", id = 3, idMere = 0, nom = "Alimentaire" });
            //var categs = Helper.IsolatedStorage.Load<ObservableCollection<CategorieViewModel>>("categories");
            //if (categs == null) categs = new ObservableCollection<CategorieViewModel>();
            //foreach (var categ in list)
            //{
            //    CategorieViewModel cvm = new CategorieViewModel() { Details = categ.details, Id = categ.id, IdMere = categ.idMere, Nom = categ.nom, IsDataLoaded = false };
            //    ListCategories.Add(cvm);
            //    if (categs.SingleOrDefault(x => x.Id == cvm.Id) == null)
            //    {
            //        categs.Add(cvm);
            //    }
            //}
            //Helper.IsolatedStorage.Save<ObservableCollection<CategorieViewModel>>(categs, "categories");
            //IsDataLoaded = true;
        }

        private void searchArticles()
        {
            ListArticles.Clear();
            //var articles = Helper.IsolatedStorage.Load<ObservableCollection<ArticleViewModel>>("articles");
            //if (articles == null) articles = new ObservableCollection<ArticleViewModel>();
            //var arts = list.Where(x => x.Nom.Contains(Search));
            //foreach (var art in arts)
            //{
            //    ArticleViewModel avm = new ArticleViewModel() { Details = art.Details, IdArticle = art.IdArticle, Nom = art.Nom, Prix = art.Prix };
            //    if (ListArticles.SingleOrDefault(x => x.IdArticle == avm.IdArticle) == null)
            //    {
            //        ListArticles.Add(avm);
            //    }
            //    if (articles.SingleOrDefault(x => x.IdArticle == avm.IdArticle) == null)
            //    {
            //        articles.Add(avm);
            //    }
            //}
            //Helper.IsolatedStorage.Save<ObservableCollection<ArticleViewModel>>(articles, "articles");
            var articles = Helper.IsolatedStorage.Load<ObservableCollection<ArticleViewModel>>("articles");
            if (articles == null) articles = new ObservableCollection<ArticleViewModel>();
            repo.GetArticlesByNom(Search, delegate(string resu, Exception e)
            {
                var des = JsonConvert.DeserializeObject<List<DBO_ShopMob.Article>>(resu);
                foreach (var art in des)
                {
                    ArticleViewModel avm = new ArticleViewModel() { Details = art.details, IdArticle = art.id, Nom = art.nom, Prix = art.prix, IsDataLoaded = false };
                    ListArticles.Add(avm);
                    if (articles.SingleOrDefault(x => x.IdArticle == avm.IdArticle) == null)
                    {
                        articles.Add(avm);
                    }
                }
                Helper.IsolatedStorage.Save<ObservableCollection<ArticleViewModel>>(articles, "articles");
            });
        }
    }
}